var bannerNew = function () {
	
	var handleValidation1 = function() {
		var form1 = $('#submit_form');
        var error1 = $('.alert-danger', form1);
        var success1 = $('.alert-success', form1);
        
        form1.validate({
    		focusCleanup: true, //focus对应对象时隐藏错误提示
    		focusInvalid: false,//验证后让未通过验证的第一个表单元素获得焦点
    		errorClass: "help-block",
    		validClass: "valid-ok",
    		errorElement: "span",
    		submitHandler: function(form){ //当表单验证成功并提交时执行
    			   			
        		
    			for( instance in CKEDITOR.instances ){ CKEDITOR.instances[instance].updateElement(); } $(form).ajaxSubmit({
    				       				
    				  success: function(res) {
    				/*
    				 a = JSON.parse(res);
					 if(a.response.data){
 						msg = "添加成功";
 				         //bootbox.alert("修改成功！")       				         
 				        
 					 }else{
 						 msg = "添加失败！";
 						 //bootbox.alert("修改失败！"); 
 						      						
 					 } 
     					 */
     					 bootbox.dialog({
			                    message: '添加成功！',
			                    //message: msg,
			                    title: "",
			                    buttons: {
			                      success: {
			                        label: "OK",
			                        className: "green",
			                        callback: function() {
				        	 			window.location.href = '/banner/lists/get';
			                        }
			                      }
			                    }
			                }); 					
    				    
    				},
    					
    				  error: function (XmlHttpRequest, textStatus, errorThrown) {
    					  console.log(errorThrown);
    					},  
    			    url:'/banner/newresource/post', /*设置post提交到的页面*/  
    			    type: 'post', /*设置表单以post方法提交*/
    			    resetForm: false
    				});
    		},
    		rules: {
    		 
              img: {
            	  required: true
              }
    		  
    		},
    		messages: {
            img: {
          	  required: '请上传图片'
            }
  		  
  		},
    		invalidHandler: function (event, validator) { //display error alert on form submit              
    			success1.hide();
                error1.show();
                App.scrollTo(error1, -200);
            },

            highlight: function (element) { // hightlight error inputs
                $(element)
                    .closest('.form-group').addClass('has-error'); // set error class to the control group
            },

            unhighlight: function (element) { // revert the change done by hightlight
                $(element)
                    .closest('.form-group').removeClass('has-error'); // set error class to the control group
            },

            success: function (label) {
                label
                    .closest('.form-group').removeClass('has-error'); // set success class to the control group
            }
    		
    	});
	}
		
    	
		var thumbImage = function () {

			$('#bizconfirm').click(function() {
            	
        		x=$("#crop_x").val();
        		y=$("#crop_y").val();
        		w=$("#crop_w").val();
        		h=$("#crop_h").val();
        		original_src=$("#original_src").val();
        		if (!parseInt($('#crop_w').val())){
        			alert('请选择裁剪区域');
                    return false;
        		}
        		$.ajax({
                    type: "POST",
                    url: '/banner/image/image_post',
                    data: {x:x,y:y,w:w,h:h,original_src:original_src},
                   
                    error: function(){  
    					alert('fail');  
    				},  
                    success: function(msg){
    					 eval('res=' + msg + ';');
    					    					
    					if( res.status == '1' ){
    						alert("缩略图生成成功");
    						$('#img').val(res.url);   						
    						$("#img").focus();
    						$("#img").blur();
    						$("#preview").empty();
    						$("#preview").html('<img width="480px" height="208px" src="'+res.url+'"></img>');
    					}else{
    						
    						alert("缩略图生成失败");
    					}		
                        
                    }
                });
        		
        		// return false;
       
              });
		}
		
		
		
		
	var cancelButton = function() {
		$('#cancel').click(function() {
			window.location.href = '/banner/lists/get';
		});

	}
	
	var cropActivityImg = function() {
        // Create variables (in this scope) to hold the API and image size
        var jcrop_api,
            boundx,
            boundy,
            // Grab some information about the preview pane
            $preview = $('#preview-pane'),
            $pcnt = $('#preview-pane .preview-container'),
            $pimg = $('#preview-pane .preview-container img'),

            xsize = $pcnt.width(),
            ysize = $pcnt.height();
        	
            console.log('init',[xsize,ysize]);

            
            
        $('#preview_activity_img').Jcrop({
          bgColor: 'white',
          addClass:'crop-border',
          onChange: updatePreview,
          onSelect: updatePreview,
          boxWidth: 400,
          boxHeight: 400,
          //setSelect:[100, 100, 150,150],  
         // aspectRatio: xsize / ysize
          aspectRatio: 1920/833
        },function(){
          // Use the API to get the real image size
          var bounds = this.getBounds();
        
          boundx = bounds[0];
          boundy = bounds[1];
          // Store the API in the jcrop_api variable
          jcrop_api = this;
          // Move the preview into the jcrop container for css positioning
          $preview.appendTo(jcrop_api.ui.holder);
        });
        
        
       

        function updatePreview(c)
        {  
        	
        	var bounds = this.getBounds();            
            boundx = bounds[0];
            boundy = bounds[1];
        	
          if (parseInt(c.w) > 0)
          {
            var rx = xsize / c.w;
            var ry = ysize / c.h;

            $pimg.css({
              width: Math.round(rx * boundx) + 'px',
              height: Math.round(ry * boundy ) + 'px',
              marginLeft: '-' + Math.round(rx * c.x) + 'px',
              marginTop: '-' + Math.round(ry * c.y) + 'px'
            });
          }
          
          $('#crop_x').val(c.x);
          $('#crop_y').val(c.y);
          $('#crop_w').val(c.w);
          $('#crop_h').val(c.h);
        };
               
        
        $('#activity_img_upload').click(function(e) {
            var src = document.getElementById("thumb_avatar").src;
            $("#preview_area").css('display','block');
            $("#original_src").val(src);
            jcrop_api.setImage(src);
            jcrop_api.setOptions({ bgOpacity: .6 });
            return false;
          });
        
        
       
    }
    return {
        //main function to initiate the module

        init: function () {
    	
    		handleValidation1();
    		thumbImage();
    		cancelButton();
    		cropActivityImg();
    		
        },
    };

}();